<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" charset="utf-8" src="./appframework.min.js"></script>
<script type="text/javascript" charset="utf-8" src="./js/base.js"></script>
<meta charset="UTF-8">
<title>心愿墙</title>
<script type="text/javascript">
$(document).ready(function(){ 
	var parm = getRequestParam();
	if(parm["code"] !=undefined && parm["code"] !="" && parm["productionIds"] !=undefined && parm["productionIds"] !=""){
		checkIsGuanzhu123(parm);
	}else{
		alert(1);
	}
});

function checkIsGuanzhu123(parm){
	$.ajax({ 
        type: "POST",
        async: false, 
        url: baseUrl + "/json/wishwall/wishOrder_checkIsGuanzhu", 
        dataType: "json", 
        data : "code="+parm["code"],
        error: function (XMLHttpRequest, textStatus, errorThrown){ 
            alert("error!"+errorThrown); 
        },
        success: function (data) {
        	if(data.state == '1'){
        		wechatIdStr = data.openId;
        		if(data.subscribe == '1'){
        			var wishOrderId = "";
        			if(checkXuYuanButton()){
        				wishOrderId = saveWishOrder2(parm);
        				window.location.href = baseUrl + "/html/index.html?wishOrderId="+wishOrderId+"&type=xuyuam#tree";
        			}else{
        				window.history.back();
        			}
        			
        		}else if(data.subscribe == '0'){
        			window.location.href = "http://mp.weixin.qq.com/s?__biz=MjM5ODIxMjAyOQ==&mid=200286198&idx=1&sn=2eea8afa8ef81515f5e74f8daada9db0#rd";
        		}
        	}else if(data.state == '0'){
        		alert("faile!");
        	}
        }
	});
}

function saveWishOrder2(parm){
	var paramStr = JSON.stringify({wechatId: wechatIdStr,programCode:programCodeVal,productionIds:parm["productionIds"]});  
	var orderId = 0;
	$.ajax({ 
        type: "POST",
        async: false, 
        url: baseUrl + "/json/wishwall/wishOrder_saveWishOrder", 
        dataType: "json",
        contentType: "application/json; charset=utf-8",  
        data : paramStr,
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
        	alert("error");
            alert(errorThrown); 
        },
        success: function (data) {
        	if("saveError" == data.msg){
        		alert("系统出错，请稍后重试。");
        	}else{
        		//返回订单id
            	orderId = data.state;	
        	}
        } 
	});
	return orderId;
}

function checkXuYuanButton(){
	var flag = false;
	$.ajax({ 
        type: "get",
        async: false, 
        url: baseUrl + "/json/wishwall/wishOrder_checkXuYuanButton", 
        dataType: "json", 
        data: "wechatId=" + wechatIdStr + "&programCode=" + programCodeVal,
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert(errorThrown); 
        },
        success: function (data) {
        	if("hadExists" == data.msg){
        		alert("您已有许愿单，请不要重复许愿，谢谢");
        	}else if("saveError" == data.msg){
        		alert("系统出错，请稍后重试。");
        	}else if("saveSuccess" == data.msg){
        		flag = true;
        	}
        } 
	});
	return flag;
}
</script>
</head>
<body>

</body>
</html>